package com.example.foodisimportant.Canteens;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodisimportant.Adapter.ViewAdapter;
import com.example.foodisimportant.Bean.PostEntity;
import com.example.foodisimportant.Body.ResponseBody;
import com.example.foodisimportant.Network.NetworkSettings;
import com.example.foodisimportant.Network.SearchNetworkThread;
import com.example.foodisimportant.R;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class SellerInformationActivity extends AppCompatActivity {
    private ImageButton mIbbacktohome2;
    private TextView mTvsellername,mTvsellerposition,mTvsellersimpleinformation;
    private RecyclerView mRvdishofseller;
    private RatingBar mRtrank;
    private ViewAdapter dishodsellerAdapter;
    private ArrayList<PostEntity> dishList=new ArrayList<PostEntity>();
    private final ObjectMapper mapper = new ObjectMapper();
    static String dishget="1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seller_information);
        mIbbacktohome2=findViewById(R.id.backtohome2);
        mTvsellername=findViewById(R.id.sellername);
        mTvsellerposition=findViewById(R.id.tv_position);
        mTvsellersimpleinformation=findViewById(R.id.simpleinformation);
        mRvdishofseller=findViewById(R.id.rv_dishlist);
        initRecyclerView();
        new Thread(new Runnable() {
            @Override
            public void run() {
                FutureTask<String> searchTask = new FutureTask<>(new SearchNetworkThread(dishget, NetworkSettings.SIGN_IN));
                Thread thread =new Thread(searchTask);
                thread.start();
                try{
                    ResponseBody body = mapper.readValue(searchTask.get(), ResponseBody.class);
                    JSONArray array=new JSONArray(body);
                    for (int i=0;i<array.length();i++){
                        JSONObject object=array.getJSONObject(i);
                        String dishName=object.getString("dishName");
                        String dishPosition=object.getString("dishPosition");
                        String dishstrImg=object.getString("dishstrImg");
                        PostEntity postEntity=new PostEntity();
                        postEntity.setName(dishName);
                        postEntity.setPosition(dishPosition);
                        postEntity.setPrice("7元");
                        postEntity.setNumber("TOP"+i);
                        postEntity.setPicture(dishstrImg);
                        dishList.add(postEntity);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
        mIbbacktohome2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }



    /**
     * 对recycleview进行设置
     */
    private void initRecyclerView(){
        //创建adapter
        dishodsellerAdapter= new ViewAdapter(SellerInformationActivity.this, dishList);
        //给RecyclerView设置adapter
        mRvdishofseller.setAdapter(dishodsellerAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mRvdishofseller.setLayoutManager(new LinearLayoutManager(SellerInformationActivity.this,LinearLayoutManager.VERTICAL,false));
        //设置item的分割线
        mRvdishofseller.addItemDecoration(new DividerItemDecoration(SellerInformationActivity.this,DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
        dishodsellerAdapter.setOnItemClickListener(new ViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, PostEntity data) {
                Intent intent=new Intent(SellerInformationActivity.this, DishInformationActivity.class);
                startActivity(intent);
            }
        });
    }
}
